package com.zwj.controller;


import com.zwj.bo.*;
import com.zwj.entity.UserEntity;
import com.zwj.service.UserService;
import com.zwj.utils.TokenUtils;
import com.zwj.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author zwj
 * @since 2021-07-21
 */
@RestController
@RequestMapping("/user")
public class UserController{
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }
    @ApiOperation(value = "登录", notes = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginBo userLoginBo,HttpServletRequest request) {
//        String rightCode = (String) request.getSession().getAttribute(userLoginBo.getCodeId());
//        System.out.println(rightCode);
//        if (rightCode == null){
//            return Result.error("验证码已失效请刷新！");
//        }
//        String inputCode = userLoginBo.getCode();
//        if (!rightCode.equalsIgnoreCase(inputCode)) {
//            return Result.error("验证码错误！");
//        }
        return userService.login(userLoginBo);
    }
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @GetMapping("/getUserDate")
    public Result getUserDate(HttpServletRequest request) {
        UserEntity userEntity = TokenUtils.getUser(request);
        return userService.getUserDate(userEntity.getId());
    }
    @ApiOperation(value = "登出", notes = "登出")
    @PutMapping("/loginout")
    public Result loginout(HttpServletRequest request) {
        UserEntity userEntity = TokenUtils.getUser(request);
        return userService.loginout(userEntity.getId());
    }
    @ApiOperation(value = "添加教师", notes = "添加教师")
    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody AddTeacherBo addTeacherBo, HttpServletRequest request) {
        UserEntity userEntity = TokenUtils.getUser(request);
        return userService.addTeacher(addTeacherBo,userEntity);
    }
    @ApiOperation(value = "停用启用教师", notes = "停用启用教师")
    @PostMapping("/updaTeacherDelFlag")
    public Result updaTeacherDelFlag(@RequestBody UpdateTeacherDelFlagBo updateTeacherDelFlagBo, HttpServletRequest request) {
        UserEntity userEntity = TokenUtils.getUser(request);
        return userService.updateTeacherDelFlag(updateTeacherDelFlagBo,userEntity);
    }
    @ApiOperation(value = "编辑教师", notes = "编辑教师")
    @PostMapping("/updaTeacher")
    public Result updaTeacher(@RequestBody UpdateTeacherBo updateTeacherBo, HttpServletRequest request) {
        UserEntity userEntity = TokenUtils.getUser(request);
        return userService.updaTeacher(updateTeacherBo,userEntity);
    }
    @ApiOperation(value = "管理员修改密码", notes = "管理员修改密码")
    @PostMapping("/adminUpdatePass")
    public Result adminUpdatePass(@RequestBody UpdatePassWordBo updatePassWordBo, HttpServletRequest request) {
        UserEntity userEntity = TokenUtils.getUser(request);
        return userService.adminUpdatePass(updatePassWordBo,userEntity);
    }

    @ApiOperation(value = "学生修改密码", notes = "学生修改密码")
    @PostMapping("/studentUpdatePass")
    public Result studentUpdatePass(@RequestBody UpdatePassWordBo updatePassWordBo, HttpServletRequest request) {
        UserEntity userEntity = TokenUtils.getUser(request);
        return userService.studentUpdatePass(updatePassWordBo,userEntity);
    }

    @ApiOperation(value = "教师修改密码", notes = "教师修改密码")
    @PostMapping("/teacherUpdatePass")
    public Result teacherUpdatePass(@RequestBody UpdatePassWordBo updatePassWordBo, HttpServletRequest request) {
        UserEntity userEntity = TokenUtils.getUser(request);
        return userService.studentUpdatePass(updatePassWordBo,userEntity);
    }

    @ApiOperation(value = "管理员重置密码", notes = "管理员重置密码")
    @PostMapping("/adminResetPass")
    public Result adminResetPass(@RequestBody ResetPassBo resetPassBo, HttpServletRequest request) {
        UserEntity userEntity = TokenUtils.getUser(request);
        return userService.adminResetPass(resetPassBo,userEntity);
    }

    @ApiOperation(value = "条件查询教师", notes = "条件查询教师")
    @PostMapping("/selectTeachers")
    public Result selectTeachers(@RequestBody SelectTeacherBo selectTeacherBo, HttpServletRequest request) {
        return userService.selectTeachers(selectTeacherBo,null);
    }
    @ApiOperation(value = "id查询指定教师", notes = "id查询指定教师")
    @PostMapping("/selectTeacherById")
    public Result selectTeacherById(@RequestBody SelectByIdBo selectByIdBo, HttpServletRequest request) {
        return userService.selectTeachers(new SelectTeacherBo(), selectByIdBo.getId());
    }
}

